#include<iostream>  
using namespace std;
int main() {
	int m, n, s, r, c;
	cin >> s;
	while (s--) {
		cin >> m >> n;
		r = m;
		c = 0;
		while (r >= n) {
			c += r / n;
			r = r / n + r % n;
		}
		cout << m + c << endl;
	}
	return 0;
}
